const cartDao = require('../dao/cartDao');
const orderDao = require('../dao/orderDao');
const productDao = require('../dao/productDao');
const checkLogin = require('../middleware/checkLogin');


module.exports = {
    getOrder: async ctx => {
        let { user_id } = ctx.request.query;
        // 校验用户是否登录
        if (!checkLogin(ctx, user_id)) {
            return;
        }
        // 1.查询该用户的所有订单
        const allOrder = await orderDao.getAllOrder(user_id);// [{order_id},{order_id}]
        console.log(allOrder)
        const len = allOrder.length;
        if(!len) {
            ctx.body = {
                status: 80200,
                message: 'success',
                data: {
                    code: '2',
                    msg: '无该用户的订单信息'
                }
            }
            return
        }
        // 获取所有订单的详细信息
        const orders = await orderDao.getOrder(user_id);
        console.log(orders)
        const orderList = [];
        for( let i = 0; i < len; i++) {
            // 订单编号
            const orderId = allOrder[i];
            const tempOrder = [];
            for(let j = 0; j < orders.length; j++) {
                // {id,order_id,user_id,product_id,product_num,product_price,order_time}
                const order = orders[j];
                if(orderId.order_id === order.order_id) {
                    // 组装商品的详细信息
                    const product = await productDao.getProductDetailsById(order.product_id);
                    order.product_name = product[0].product_name;
                    order.product_picture = product[0].product_picture;
                    tempOrder.push(order)
                }
            }
            orderList.push(tempOrder)
        }
        ctx.body = {
            status: 80200,
            message: 'success',
            data: orderList
        }
    },
    /**
     * 新增订单
     * @param {*} ctx 
     * @returns 
     */
    addOrder: async ctx => {
        const { user_id, products } = ctx.request.body;
        // 校验用户是否登录
        if (!checkLogin(ctx, user_id)) {
            return;
        }
        // 1. 获取当前时间
        const tempTime = new Date().getTime();
        // 2. 生产订单id 用户id+时间
        const order_id = +("" + user_id + tempTime)
        // 3. 循环遍历订单数据生产订单表结构数据
        const data = [];
        const len = products.length;
        for (let i = 0; i < len; i++) {
            const temp = products[i];
            data.push(order_id, user_id, temp.productID, temp.num, temp.price, tempTime)
        }
        // 4. 将订单信息插入数据库
        try {
            const result = await orderDao.addOrder(len, data);
            if (result.affectedRows === len) {
                // 5.订单新增成功后，删除购物车
                let rows = 0;
                for (let index = 0; index < len; index++) {
                    const temp = products[index];
                    const result = await cartDao.deleteCart(user_id, temp.productID);
                    rows += result.affectedRows
                }
                // 6.判断购物车删除是否成功
                if (rows != len) {
                    ctx.body = {
                        status: 80200,
                        message: 'success',
                        data: {
                            code: '2',
                            msg: '购买成功，但购物车更新失败'
                        }
                    }
                    return
                }
                ctx.body = {
                    status: 80200,
                    message: 'success',
                    data: {
                        code: '1',
                        msg: '购买成功'
                    }
                }
            } else {
                ctx.body = {
                    status: 80200,
                    message: 'success',
                    data: {
                        code: '4',
                        msg: '购买失败'
                    }
                }
            }
        } catch (error) {
            return Promise.reject(error)
        }
    }
}